package com.xmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xmall.bean.table.Keyword;
import com.xmall.bean.table.KeywordExample;
import com.xmall.bean.vo.IssueList;
import com.xmall.mapper.KeywordMapper;
import com.xmall.service.KeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public IssueList<Keyword> selectKeyword(Integer page, Integer limit, String keyword, String url, String sort, String order) {
        KeywordExample keywordExample = new KeywordExample();
        IssueList<Keyword> issueList = new IssueList<>();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        if(keyword != null){

            criteria.andKeywordLike("%" + keyword + "%");
        }
        if(url != null){
            criteria.andUrlEqualTo(url);
        }
        criteria.andDeletedEqualTo(false);
        keywordExample.setOrderByClause(sort + " " +order);

        PageHelper.startPage(page,limit);

        PageInfo<Keyword> keywordPageInfo = new PageInfo<>();
        long total = keywordPageInfo.getTotal();
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);

        issueList.setItems(keywords);
        issueList.setTotal(total);

        return issueList;
    }

    @Override
    public Keyword insertKeyword(Keyword keyword) {
        keyword.setUpdateTime(new Date());
        keyword.setAddTime(new Date());

        keywordMapper.insertSelective(keyword);
        return keyword;
    }

    @Override
    public Keyword updateKeyword(Keyword keyword) {

        keyword.setUpdateTime(new Date());
        keywordMapper.updateByPrimaryKeySelective(keyword);

        return keyword;
    }

    @Override
    public Integer deleteKeyword(Keyword keyword) {

        keyword.setDeleted(true);
        keywordMapper.updateByPrimaryKey(keyword);
        return 0;
    }

}
